<template>
  <div>
    <el-dialog title="取消订单" :visible.sync="dialogVisible" width="600px" @close="close">
      <el-form
        v-if="detail"
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        label-suffix="："
        label-width="100px"
        size="small"
      >
        <el-form-item label="退款操作" prop="refundOperateType">
          <el-radio-group v-model="form.refundOperateType">
            <el-radio v-show="allDetail.refundOperateType == 2" :label="2">扣除订单实付金额的{{ allDetail.refundOperateRatio }}%</el-radio>
            <el-radio v-show="allDetail.refundOperateType == 1" :label="1">取消订单不退款</el-radio>
            <el-radio :label="0">全部退回</el-radio>
          </el-radio-group>
          <div class="redTip">
            <span>{{ labelTextObj['refundText'] }}</span>
          </div>
        </el-form-item>
        <el-form-item label="取消原因" prop="type">
          <div>
            <el-radio v-model="form.type" :label="1">同房型没有连续的空余(请选择没有房间的日期)</el-radio>
          </div>
          <el-card v-if="form.type===1">
            <div>
              <el-checkbox-group v-model="form.days">
                <el-checkbox v-for="(v,k,i) in detail.bizData.roomPrices" :key="i" :label="k">{{ k }}</el-checkbox>
              </el-checkbox-group>
            </div>
          </el-card>
          <div>
            <el-radio v-model="form.type" :label="2">其它原因</el-radio>
          </div>
          <el-card v-if="form.type===2">
            <el-form-item prop="cancelReason">
              <el-input v-model.trim="form.cancelReason" maxlength="30" placeholder="请输入原因" />
            </el-form-item>
          </el-card>
          <span class="redTip">{{ labelTextObj['reasonText'] }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" :loading="loading" @click="sumbit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderDetail, changeOrderStatus } from '../../api/record'
import { markProductStatus } from '../../api/product'
import confirmMessage from '@/components/ConfirmMessage/index'
export default {
  data() {
    return {
      /**
         * 校验字段相关
         */
      rules: {
        refundOperateType: [{ required: true, message: '请选择退款操作', trigger: ['blur', 'change'] }],
        type: [
          {
            required: true,
            message: '请选择取消原因类型'
          }
        ],
        cancelReason: [
          {
            required: true,
            message: '请输入取消原因'
          }
        ]
      },
      // 配置
      subTypeOptions: {
        0: '全日房',
        1: '钟点房',
        2: 'KTV房'
      },
      resolve: null,
      reject: null,
      dialogVisible: null,
      loading: false,
      // 数据
      detail: '',
      allDetail: {},
      form: {
        type: 1,
        days: [],
        cancelReason: '',
        refundOperateType: ''
      }
    }
  },
  computed: {
    labelTextObj() {
      const { payFee, bizData: { alias }, title } = this.detail
      const { refundOperateRatio, surPayAmt, refundOperateType: detailRefundOperateType } = this.allDetail
      const { refundOperateType } = this.form
      // 配送方式文案
      const distributionText = alias || title
      // 默认文案： refundOperateType == 0 || 初始状态
      const obj = {
        refundText: `注：当前用户可免费取消${distributionText}订单`,
        reasonText: `*确认取消将向会员原路退回实收金额${payFee / 100}元`
      }
      console.log('distributionText', distributionText)
      if (refundOperateType == 2) {
        // 取消订单需扣除费用
        obj['reasonText'] = `*确认取消将向会员原路退回实收金额${surPayAmt / 100}元`
      } else if (refundOperateType == 1) {
        // 配置为不可取消
        obj['reasonText'] = `*确认取消将不向会员退回实收金额`
      }

      // refundText 根据订单refundOperateType修改
      if (detailRefundOperateType == 2) {
        // 取消订单需扣除费用
        obj['refundText'] = `注：当前用户取消${distributionText}订单需扣除订单实付金额的${refundOperateRatio}%`
      } else if (detailRefundOperateType == 1) {
        // 配置为不可取消
        obj['refundText'] = `注：当前用户无法取消${distributionText}订单`
      }
      return obj
    }
  },
  methods: {
    open(detail) {
      this.detail = detail
      this.allDetail = detail
      console.log('detail === ', detail)
      this.form.refundOperateType = detail.refundOperateType
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    sumbit() {
      if (this.allDetail.surPayAmt < 0) {
        this.$message.error('可退款金额不足！')
        return
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log('this.detail', this.detail)
          this.detail.fapiaoSts && confirmMessage({ title: '退款发票自动冲红提示', center: true })
            .then(() => {
              this.sumbitInfo()
            })
            .catch(() => {
              console.log('我是取消')
            })
          !this.detail.fapiaoSts && this.sumbitInfo()
        }
      })
    },

    sumbitInfo() {
      const params = {
        orderNo: this.detail.orderNo,
        status: 0,
        refundOperateType: this.form.refundOperateType,
        refundOperateRatio: this.allDetail.refundOperateRatio
      }
      switch (this.form.type) {
        case 1:
          params.cancelReason = '同房型没有连续的空余'
          var checkArr = []
          for (const key in this.daysChecked) {
            if (this.daysChecked[key].check) checkArr.push(key)
          }
          break
        case 2:
          params.cancelReason = this.form.cancelReason
          break
      }
      this.loading = true
      changeOrderStatus(params)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '订单取消成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.resolve && this.resolve()
          if (this.form.type === 1 && checkArr.length !== 0) {
            this.markStatus()
          }
          this.hide()
        })
        .catch(err => {})
        .finally(() => {
          this.loading = false
        })
    },

    // 标记商品状态
    markStatus() {
      const params = {}
      params.days = checkArr
      params.status = 0
      params.productNo = this.detail.subLabel
      markProductStatus(params)
        .then(res => {})
        .catch(err => {})
        .finally(() => {})
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

  .redTip{
    color: #D9001B;
  }
</style>
